how to check if a string contains a substring in php

36

How do I check if a string contains a specific word php -

$a = 'How are you?';

if (strpos($a, 'are') !== false) {
    echo 'true';
}

how to check if a string contains a substring in php -

$a = 'How are you?';

if (strpos($a, 'are') !== false) {
    echo 'true';
}

check if string contains character php -

// this method is new with PHP 8 and above
$haystack = "Damn, I wonder if this string contains a comma.";
if (str_contains($haystack, ",")) {
	echo "There is a comma!!";
}

Comments

Submit
0 Comments